package com.sc.controller.patrol;

import com.sc.domain.patrol.PatrolPath;
import com.sc.dto.PatrolPathDTO;
import com.sc.result.PageResult;
import com.sc.result.Result;
import com.sc.service.patrol.PatrolPathService;
import com.sc.vo.PatrolPathVO;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/patrolPath")
@RequiredArgsConstructor
public class PatrolPathController {

    private final PatrolPathService patrolPathService;

    @PostMapping("/addPatrolPath")
    public Result addPatrolPath(@RequestBody PatrolPath patrolPath) {
        patrolPath.setAddTime(LocalDateTime.now());
        try {
            return Result.success("添加成功", patrolPathService.save(patrolPath));
        } catch (DuplicateKeyException e) {
            return Result.error("巡更路线名称重复，换一个试试");
        }
    }

    @DeleteMapping("/deletePatrolPath")
    public Result deletePatrolPath(Integer id) {
        return Result.success("删除成功", patrolPathService.removeById(id));
    }

    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Integer> ids) {
        return Result.success("删除成功", patrolPathService.removeByIds(ids));
    }

    @PutMapping("/updatePatrolPath")
    public Result updatePatrolPath(@RequestBody PatrolPath patrolPath) {
        try {
            return Result.success("修改成功", patrolPathService.updateById(patrolPath));
        } catch (DuplicateKeyException e) {
            return Result.error("巡更路线名称重复，换一个试试");
        }
    }

    @GetMapping("/selectPatrolPath")
    public Result selectPatrolPath(Integer id) {
        return Result.success(patrolPathService.getById(id));
    }

    @PostMapping("/listPatrolPath")
    public Result<PageResult<PatrolPathVO>> listPatrolPath(@RequestBody PatrolPathDTO patrolPathDTO) {
        return Result.success(patrolPathService.pageQuery(patrolPathDTO));
    }

    @GetMapping("/getAll")
    public Result<List<PatrolPath>> getAll() {
        return Result.success(patrolPathService.list());
    }
}
